//      panel.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "panel.h"

gboolean panel_clock_update ( GtkLabel * clock )
{
	gchar t [256] ;
	time_t t2 ;
	
	struct tm * t3 ;
	
	t2 = time ( null ) ;
	t3 = localtime ( & t2 ) ;
	
	strftime ( t, 256, " %I:%M %p ", t3 ) ;
	gtk_label_set_label ( clock, t ) ;
}

GtkWidget * panel_new ( void )
{
	wnck_set_client_type ( WNCK_CLIENT_TYPE_PAGER ) ;
	
	GtkWidget * panel = gtk_window_new ( GTK_WINDOW_TOPLEVEL ) ;
	gtk_window_set_type_hint ( panel, GDK_WINDOW_TYPE_HINT_DOCK ) ;
	
	/* Originally the "start button" was a "button". Now it's a menu
	 * bar. Funny thing, this programming is :)
	 *
	GtkButton * s1 = gtk_toggle_button_new () ;
	gtk_button_set_relief ( s1, GTK_RELIEF_NONE ) ;
	*/
			  
	GtkBox * a = gtk_vbox_new ( no, 0 ) ;
	gtk_container_add ( panel, a ) ;
	
	GtkMenuBar * mb = gtk_menu_bar_new () ;
	GtkMenuItem * mi = gtk_image_menu_item_new () ;
	
	g_signal_connect ( mi, "activate", panel_menu_button_pressed, mi ) ;
	gtk_menu_shell_append ( mb, mi ) ;
	
	gtk_widget_set_tooltip_text ( mb, "Click here to begin" ) ;
	
	GtkHBox * box = gtk_hbox_new ( no, no ) ;
	gtk_box_pack_start ( a, box, yes, no, 0 ) ;
	
	gtk_box_pack_start ( box, mb, no, no, 0 ) ;
	gtk_box_pack_start ( box, gtk_vseparator_new (), no, no, 2 ) ;
		
	GtkButton * sd = gtk_button_new () ;
	gtk_box_pack_start ( box, sd, no, no, 2 ) ;
	
	gtk_widget_set_tooltip_text ( sd, "Show desktop" ) ;
	gtk_button_set_relief ( sd, GTK_RELIEF_NONE ) ;
	
	start_unpressed = gdk_pixbuf_new_from_inline ( -1, start1, false, null ) ;
	start_pressed = gdk_pixbuf_new_from_inline ( -1, start2, false, null ) ;
	
	//gtk_container_add ( mi, gtk_image_new_from_pixbuf ( start_unpressed )) ;
	gtk_image_menu_item_set_image ( mi, gtk_image_new_from_pixbuf ( start_unpressed )) ;
	gtk_image_menu_item_set_always_show_image ( mi, true ) ;
	
	GdkPixbuf * p = gdk_pixbuf_new_from_inline ( -1, show_desktop, false, null ) ;
	GdkPixbuf * p1 = gdk_pixbuf_scale_simple ( p, 18, 18, GDK_INTERP_BILINEAR ) ;
	
	g_signal_connect ( sd, "clicked", panel_show_desktop, null ) ;
	gtk_container_add ( sd, gtk_image_new_from_pixbuf ( p1 )) ;
	
	panel_quick_launch_parse ( box ) ;
	gtk_box_pack_start ( box, gtk_vseparator_new (), no, no, 2 ) ;
	
	g_object_unref ( p ) ;
	g_object_unref ( p1 ) ;
	
	/* Taskbar: libwnck is awesome! It gives me a ready made taskbar!
	 * Hooray open source :)
	 * 
	 * Taskbar in 2 lines, sexy!
	 */
	
	GtkWidget * taskbar = wnck_tasklist_new ( wnck_screen_get_default ()) ;
	gtk_box_pack_start ( box, taskbar, yes, yes, 2 ) ;
	
	wnck_tasklist_set_include_all_workspaces ( taskbar, true ) ;
	gtk_box_pack_start ( box, gtk_vseparator_new (), no, no, 2 ) ;
	
	GtkFrame * f = gtk_frame_new ( null ) ;
	GtkBox * b = gtk_hbox_new ( no, 2 ) ;
	
	gtk_box_pack_end ( box, f, no, no, 2 ) ;
	gtk_container_add ( f, b ) ;
	
	/* Clock */
	GtkLabel * clock = gtk_label_new ( null ) ;
	gtk_box_pack_end ( b, clock, no, no, 2 ) ;
	
	/* We don't show seconds, so why update every second?
	 * We do it every 30 seconds */
	
	///| Update clock with lower priority, as the shell itself
	///| will run with the highest
	
	panel_clock_update ( clock ) ;
	g_timeout_add ( 30000, panel_clock_update, clock ) ;
	
	GtkMenu * menu = gtk_menu_new () ;
	gchar * menu_dir = g_build_filename ( g_get_home_dir (), ".system/Start Menu", null ) ;
	
	panel_menu_parse_folder ( menu, menu_dir, true ) ;
	panel_menu_add_shutdown_options ( menu ) ;
	
	gtk_widget_show_all ( menu ) ;
	
	g_signal_connect_swapped ( mi, "deselect", panel_menu_button_unpressed, mi) ;
	g_free ( menu_dir ) ;
	
	gtk_menu_item_set_submenu ( mi, menu ) ;
	
	//gtk_menu_popup ( menu, null, null, panel_menu_reposition, null, 0, gtk_get_current_event_time ()) ;
	
	gtk_window_resize ( panel, gdk_screen_width (), 27 ) ;
	gtk_window_move ( panel, 0, gdk_screen_height () - 27 ) ;
	
	return panel ;
}

void panel_show ( GtkWidget * panel )
{
	gtk_widget_show_all ( panel ) ;
	april_update_gui () ;
	
}

void panel_run ( void )
{
	GtkWidget * panel = panel_new () ;
	panel_show ( panel ) ;
}

void panel_menu_parse_file ( GtkMenu * menu, gchar * filename, gboolean is_root )
{
	GKeyFile * file = g_key_file_new () ;
	if ( ! g_key_file_load_from_file ( file, filename, G_KEY_FILE_NONE, null ))
	{
		g_key_file_free ( file ) ;
		return ;
	}
	
	gchar * name = g_key_file_get_string ( file, "Desktop Entry", "Name", null ) ;
	gchar * exec = g_key_file_get_string ( file, "Desktop Entry", "Exec", null ) ;

	gchar * icon = g_key_file_get_string ( file, "Desktop Entry", "Icon", null ) ;
	
	///> Todo: Not available in Beta
	///| gchar * icon = g_key_file_get_string ( file, "Desktop Entry", "Icon", null ) ;
	
	GtkMenuItem * item = gtk_image_menu_item_new_with_label ( name ) ;
	
	if ( is_root )
	{
		GdkPixbuf * p = panel_menu_find_icon ( icon, 32 ) ;
		
		if ( p )
		{
			gtk_image_menu_item_set_image ( item, gtk_image_new_from_pixbuf ( p )) ;
			g_object_unref ( p ) ;
		}
	}
	
	gtk_image_menu_item_set_always_show_image ( item, true ) ;
	g_free ( icon ) ;	
	
	if ( is_root )
		gtk_widget_set_size_request ( item, 100, 40 ) ;
			
	g_signal_connect_swapped ( item, "activate", panel_menu_run, exec ) ;
	
	g_free ( name ) ;
	g_key_file_free ( file ) ;
	
	gtk_menu_shell_append ( menu, item ) ;
}

void panel_menu_parse_folder ( GtkMenu * menu, gchar * folder, gboolean is_root )
{
	GError * error = null ;
	GDir * dir = g_dir_open ( folder, 0, &error ) ;
	
	if ( dir == null )
	{
		g_warning ( "Cannot open start menu folder!\n%s", error -> message ) ;
		return ;
	}
	
	PANEL_MENU_LAST_ITEM last_item = PANEL_MENU_LAST_ITEM_WAS_UNKNOWN ;
	
	gchar * name = g_dir_read_name ( dir ) ;
	while ( name )
	{
		gchar * full_name = g_build_filename ( folder, name, null ) ;
		
		if ( g_file_test ( full_name, G_FILE_TEST_IS_DIR ))
		{
			GtkMenu * m1 = gtk_menu_new () ;
			GtkMenuItem * item = gtk_image_menu_item_new_with_label ( name ) ;
	
			gchar * icon = g_strdup_printf ( "%s/_%s.png", folder, name ) ;
			if ( is_root && g_file_test ( icon, G_FILE_TEST_EXISTS ))
				gtk_image_menu_item_set_image ( item, gtk_image_new_from_file ( icon )) ;
			
			gtk_image_menu_item_set_always_show_image ( item, true ) ;
			g_free ( icon ) ;	
			
			panel_menu_parse_folder ( m1, full_name, false ) ;
			gtk_menu_item_set_submenu ( item, m1 ) ;
			
			if ( is_root )
				gtk_widget_set_size_request ( item, 100, 40 ) ;
			
			if ( is_root && last_item == PANEL_MENU_LAST_ITEM_WAS_FILE )
			{
				gtk_menu_shell_append ( menu, gtk_separator_menu_item_new ()) ;
				
			}
		
			gtk_menu_shell_append ( menu, item ) ;
			last_item = PANEL_MENU_LAST_ITEM_WAS_FOLDER ;
		}
		
		else if ( name [0] != '_' )
		{
			if ( is_root && last_item == PANEL_MENU_LAST_ITEM_WAS_FOLDER )
			{
				gtk_menu_shell_append ( menu, gtk_separator_menu_item_new ()) ;
				
			}
			
			panel_menu_parse_file ( menu, full_name, is_root ) ;
			last_item = PANEL_MENU_LAST_ITEM_WAS_FILE ;
		}
		
		g_free ( full_name ) ;
		name = g_dir_read_name ( dir ) ;
	}
	
	g_dir_close ( dir ) ;
}

void panel_menu_reposition ( GtkMenu * menu, gint * x, gint * y, gboolean push_in, gpointer data )
{
	//g_debug ( "S" ) ;
	y = gdk_screen_height () / 2 ;
	x = 200 ;
	
	push_in = false ;
}

void panel_menu_button_pressed ( GtkImageMenuItem * item )
{
	gtk_image_set_from_pixbuf ( gtk_image_menu_item_get_image ( item ), start_pressed ) ;
}

void panel_menu_button_unpressed ( GtkImageMenuItem * item )
{
	gtk_image_set_from_pixbuf ( gtk_image_menu_item_get_image ( item ), start_unpressed ) ;
}

GdkPixbuf * panel_menu_find_icon ( const gchar * name, gint size )
{
	GdkPixbuf * pixbuf = null ;
	GError * error = null ;
	
	pixbuf = gtk_icon_theme_load_icon ( gtk_icon_theme_get_default (),
										  name,
										  size,
										  GTK_ICON_LOOKUP_USE_BUILTIN |
										  GTK_ICON_LOOKUP_GENERIC_FALLBACK,
										  & error ) ;
	
	if ( pixbuf == null )
	{
		g_warning ( "Cannot find icon: %s", error -> message ) ;
		return null ;
	}
	
	return pixbuf ;
}

void panel_menu_run ( gchar * program )
{
	gchar * cmd = g_strdup_printf ( "%s &", program ) ;
	system ( cmd ) ;
	
	g_free ( cmd ) ;
}

void panel_menu_add_shutdown_options ( GtkMenu * menu )
{
	GtkImageMenuItem * l = gtk_image_menu_item_new_with_label ( "Log Off" ),
					 * s = gtk_image_menu_item_new_with_label ( "Shutdown" ) ;
					 
	GdkPixbuf * p1 = panel_menu_find_icon ( "gtk-quit", 32 ),
			  * p2 = panel_menu_find_icon ( GTK_STOCK_DIALOG_AUTHENTICATION, 32 ) ;
	
	gtk_image_menu_item_set_image ( l, gtk_image_new_from_pixbuf ( p2 )) ;
	gtk_image_menu_item_set_image ( s, gtk_image_new_from_pixbuf ( p1 )) ;
	
	g_object_unref ( l ) ;
	g_object_unref ( s ) ;

	gtk_image_menu_item_set_always_show_image ( l, true ) ;
	gtk_image_menu_item_set_always_show_image ( s, true ) ;

	gtk_menu_shell_append ( menu, gtk_separator_menu_item_new ()) ;

	gtk_menu_shell_append ( menu, l ) ;
	gtk_menu_shell_append ( menu, s ) ;
}

void panel_show_desktop ( void )
{
	wnck_screen_toggle_showing_desktop ( wnck_screen_get_default (), true ) ;
}

void panel_quick_launch_parse ( GtkBox * ql )
{
	gchar * ql_dir = g_build_filename ( g_get_home_dir (), ".system/Quick Launch", null ) ;
	GError * error = null ;
	
	GDir * dir = g_dir_open ( ql_dir, 0, & error ) ;
	if ( dir == null )
	{
		g_warning ( "%s", error -> message ) ;
		g_free ( ql_dir ) ;
		
		return ;
	}
	
	gchar * name = g_dir_read_name ( dir ) ;
	while ( name )
	{
		GKeyFile * file = g_key_file_new () ;
		gchar * full_name = g_build_filename ( ql_dir, name, null ) ;
		
		if ( ! g_key_file_load_from_file ( file, full_name, G_KEY_FILE_NONE, & error ))
		{
			g_warning ( "Cannot load quick launch icon: %s", error -> message ) ;
			error = null ;
			
			g_key_file_free ( file ) ;
			g_free ( full_name ) ;
			
			name = g_dir_read_name ( dir ) ;
			continue ;
		}
		
		gchar * Name = g_key_file_get_string ( file, "Desktop Entry", "Name", & error ) ;
		if ( error )
			april_message_box_warning ( error -> message ) ;

		gchar * Icon = g_key_file_get_string ( file, "Desktop Entry", "Icon", & error ) ;
		if ( error )
			april_message_box_warning ( error -> message ) ;
		
		gchar * Exec = g_key_file_get_string ( file, "Desktop Entry", "Exec", & error ) ;
		if ( error )
			april_message_box_warning ( error -> message ) ;
		
		GtkButton * button = gtk_button_new () ;
		GtkImage * image = gtk_image_new () ;
		
		GdkPixbuf * p = panel_menu_find_icon ( Icon, 18 ) ;
		gtk_image_set_from_pixbuf ( image, p ) ;
		
		gtk_container_add ( button, image ) ;
		g_object_unref ( p ) ;
		
		gtk_button_set_relief ( button, GTK_RELIEF_NONE ) ;
		
		g_signal_connect_swapped ( button, "clicked", panel_menu_run, Exec ) ;
		gtk_widget_set_tooltip_text ( button, Name ) ;
		
		g_free ( Name ) ;
		g_free ( Icon ) ;
		
		g_free ( full_name ) ;
		g_key_file_free ( file ) ;
		
		gtk_box_pack_start ( ql, button, no, no, 0 ) ;
		name = g_dir_read_name ( dir ) ;
		
	}
	
	g_dir_close ( dir ) ;
	g_free ( ql_dir ) ;
	
	gtk_widget_show_all ( ql ) ;
}
		
